﻿using System;

namespace Birkelund.QueryStrings.Tokens
{
    /// <summary>
    /// Class to provide querystring tokens of datatype <see cref="String"/>
    /// </summary>
    public class StringToken : QueryStringTokenBase<string>, IRenderIfNullOrEmptyValue
    {
        /// <summary>
        /// Gets or sets the value indicating whether to render the token which value is empty
        /// </summary>
        /// <value>
        /// <c>true</c> or <c>false</c>.
        /// </value>
        public virtual bool RenderIfEmptyValue { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="StringToken"/> class.
        /// </summary>
        /// <param name="keyName">The key.</param>
        public StringToken(string keyName)
            : base(keyName)
        {
            Initialize();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="key">The key</param>
        /// <param name="doKeyValidation">False to bypass validation</param>
        internal protected StringToken(string key, bool doKeyValidation) : base(key, doKeyValidation)
        {
            Initialize();
        }

        private void Initialize()
        {
            RenderIfEmptyValue = true;
        }

        /// <summary>
        /// Parses from stringValue.
        /// </summary>
        /// <param name="stringValue">The string value.</param>
        /// <returns>Returns the value. If null, string.Empty is returned</returns>
        protected override string OnParseFromString(string stringValue)
        {
            return stringValue;
        }

        protected override string OnRenderTokenValue(string value)
        {
            return value;
        }
    }
}